import { getToken, setToken, removeToken } from '@/utils/auth'
import { rolePermission, saveSetGrant, currentAndSubPermission, permissionQueryList, userRoleQueryList, setUserToRole, queryList, deletes, add, update } from '@/api/subsys/userRoleAdmin'
const userRoleAdmin = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    }
  },

  actions: {
    // 查询全部
    RoleQueryList({ commit }, datas) {
      return new Promise((resolve, reject) => {
        queryList(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 查询全部权限与子权限
    CurrentAndSubPermissionYuan({ commit }, datas) {
      return new Promise((resolve, reject) => {
        currentAndSubPermission(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 查询选中权限与子权限
    RolePermissionYuan({ commit }, datas) {
      return new Promise((resolve, reject) => {
        rolePermission(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    PermissionQueryList({ commit }, datas) {
      return new Promise((resolve, reject) => {
        permissionQueryList(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 穿梭框提交
    SetUserToRole({ commit }, datas) {
      return new Promise((resolve, reject) => {
        setUserToRole(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    SaveSetGrant({ commit }, datas) {
      return new Promise((resolve, reject) => {
        saveSetGrant(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 为穿梭右框查询用户列表
    UserRoleQueryList({ commit }, datas) {
      return new Promise((resolve, reject) => {
        userRoleQueryList(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 删除
    RoleDelete({ commit }, datas) {
      return new Promise((resolve, reject) => {
        deletes(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    /*
      新增
      data object
    */

    RoleAdd({ commit }, datas) {
      return new Promise((resolve, reject) => {
        add(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 更新
    RoleUpdate({ commit }, datas) {
      return new Promise((resolve, reject) => {
        update(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }

  }
}

export default userRoleAdmin
